


<table class="altrowstable" id="categoryListDiv">
    <thead>
    <tr>
        <th>序号</th>
        <th>产品编号</th>
        <th>产品名称</th>
        <th>数量单位</th>
        <th>分类名称</th>
        <th>添加日期</th>
        <th>销售价</th>
        <th>产品描述</th>
        <th>操作</th>

    </tr>
    </thead>
    <tbody id="tableBody"></tbody>
</table>

<div  style="position: absolute; margin-top: 0px;padding-left: 700px">

    <ul id="page">分页</ul>
</div>
<input type="button" value="添加" onclick="addProduct('upAddProduct.html')" style="margin-left: 600px ; position: absolute; margin-top: 30px; font-size: 18px ; border: #84fab0; background-color: #84fab0">




<script id="productTemplate" type="text/html">{{each data}}
<tr>
    <td >{{$index+1}}</td>
    <td>{{$value.productCode}}</td>
    <td>{{$value.name}}</td>
    <td>{{$value.unitName}}</td>
    <td>{{$value.category}}</td>
    <td>{{$value.createDate}}</td>
    <td>{{$value.price}}</td>
    <td>{{$value.remark}}</td>
    <td><span style="color: #b50f11" onclick="deleProduct(this)" id={{$value.productCode}}  >删除&nbsp;&nbsp;</span><span style="color: #4a5fb5;" onclick="updateProduct('upAddProduct.html','{{$value.productCode}}')">修改</span></td>
</tr>
{{/each}}
</script>
<script>



    function addProduct(url) {
        Ajax.getHtml(url,
            function (html) {
            Ajax.post("/addProduct",null,
            function (result) {
                $('#productCode').val(result.data.productCode);
                var str = template("productCategory", result);
                document.getElementById('categoryBody').innerHTML = str;
            });

                layer.open({
                    type:1,
                    title:'产品添加',
                    area: ['800px',['400px']],
                    content: html,
                    btn: ['确认', '取消'],
                    yes :function () {
                        Ajax.post("/addProduct2",
                            {
                                productCode:$('#productCode').val(),
                                name:$('#productName').val(),
                                unitName:$('#unitName').val(),
                                remark:$('#remark').val(),
                                price:$('#price').val(),
                                categoryID:$('#product').find("option:selected").val()
                            },
                            function (result) {
                                doResult(result, function () {
                                    layer.closeAll()
                                    show('product.html')
                                    layer.alert(result.msg)
                                })
                            }
                        )
                    }
                })
        })



    }



    function updateProduct(url,productCode){
        Ajax.getHtml(url,
        function (html) {
            Ajax.post("/updateProduct",
                {productCode:productCode},
            function (result) {
                $('#productCode').val(result.data.productCode);
                $('#productName').val(result.data.name);
                $('#unitName').val(result.data.unitName);
                $('#remark').val(result.data.remark);
                $('#price').val(result.data.price);
                var str = template("productCategory", result);
                document.getElementById('categoryBody').innerHTML = str;
                var category=result.data.categoryID;
                var product =document.getElementById("product");
                for (var i=0;i<product.options.length;i++){
                    if (product.options[i].value==category){
                        product.options[i].selected=true;
                    }
                }
            });

            layer.open({
                type:1,
                title:'产品信息修改',
                area: ['800px',['400px']],
                content: html,
                btn: ['确认', '取消'],
                yes :
                    function () {
                    Ajax.post("/updateProduct2",
                            {
                                productCode:$('#productCode').val(),
                                name:$('#productName').val(),
                                unitName:$('#unitName').val(),
                                remark:$('#remark').val(),
                                price:$('#price').val(),
                                categoryID:$('#product').find("option:selected").val()
                            },
                        function (result) {
                            doResult(result, function () {
                                layer.closeAll();
                                show('product.html');

                            })
                        }
                    )
                }
            })
        })
    }






function deleProduct(Obj){
    var flat= window.confirm('确认删除?');

    var productCode = Obj.id;
    if (flat==true){
        Ajax.post("/deleteProduct",
            {productCode:productCode},
            function (result) {
                doResult(result,function () {
                    show("product.html")
                })
            })
    } else {
        return false;
    }

}


    $(function () {
        showPage({
            currentPage: 1,
            pageSize: 3,
            url: '/productList',
            templateId: 'productTemplate',
            contentId: 'tableBody',
        })
    })
</script>